
import { ROOT } from '@sker/core';
import { AmqpConnectionManager, connect } from 'amqp-connection-manager';
import { join } from 'path';
import { DependencyContainer } from 'tsyringe';
export interface MqHandler {
    (conn: AmqpConnectionManager): Promise<void>;
}
export const useMqManager = async (injector: DependencyContainer, handler: MqHandler) => {
    const root = injector.resolve(ROOT)
    const jsonFile = join(`${root}`, 'data/plugins', `@sker/mq.json`)
    const config = require(jsonFile)
    const con = connect(config.url)
    con.on('error', console.error)
    con.on('connectFailed', () => con.reconnect())
    const res = await handler(con);
    await con.close();
    return res;
}
export default useMqManager;
